package edu.gatech.cs2340.teamrocket.control.networking.impl;

import java.io.IOException;
import java.net.UnknownHostException;
import java.net.InetAddress;
import edu.gatech.cs2340.teamrocket.control.networking.node.Client;
import edu.gatech.cs2340.teamrocket.control.networking.node.Server;
import edu.gatech.cs2340.teamrocket.control.networking.exception.TimeoutException;
import edu.gatech.cs2340.teamrocket.control.networking.impl.message.ChatMessage;
import edu.gatech.cs2340.teamrocket.control.networking.impl.messagehandler.VTClientMessageHandler;
import edu.gatech.cs2340.teamrocket.control.networking.impl.messagehandler.VTServerMessageHandler;
import edu.gatech.cs2340.teamrocket.control.networking.message.Message;
import edu.gatech.cs2340.teamrocket.control.networking.node.Node;
import edu.gatech.cs2340.teamrocket.model.Model;
import edu.gatech.cs2340.teamrocket.view.networking.ChatDisplay;

/**
 * Links Networking classes to main game classes. Allows only certain actions
 * to take place.
 *
 * @author Joe Tacheron
 */
public class VTNetworkingInterface {

    private static final String LOOPBACK = "127.0.0.1";
    private static final int MAX_CONNECTIONS = 5;
    private static final int PORT_NUM = 4444;

    private static VTNetworkingInterface instance;

    public static VTNetworkingInterface getInstance(){
        if(instance == null)
            instance = new VTNetworkingInterface();
        return instance;
    }

    private VTClientMessageHandler clientHandler;
    private VTServerMessageHandler serverHandler;

    private Server server;
    private Client client;

    private Model model;

    private VTNetworkingInterface(){
        Node.setPortNum(PORT_NUM);
        serverHandler = new VTServerMessageHandler();
        clientHandler = new VTClientMessageHandler();
        server = new Server(serverHandler, MAX_CONNECTIONS);
        client = new Client(clientHandler);
        model = Model.getInstance();
    }

    public Message startServer(String username, String password)
            throws UnknownHostException, IOException, TimeoutException {
        server.start(password);
        return client.login(LOOPBACK, username, password);
    }

    public Message joinServer(String host, String username, String password)
            throws UnknownHostException, IOException, TimeoutException {
        return client.login(host, username, password);
    }

    public void setChatDisplay(ChatDisplay disply){
        clientHandler.setChatDisplay(disply);
    }

    public void sendChatMessage(String s){
        client.send(new ChatMessage(model.getUsername(), s));
    }

    public boolean clientIsRunning(){
        return client.isRunning();
    }

    public boolean serverIsRunning(){
        return server.isRunning();
    }

    public void closeClient(){
        client.close();
    }

    public void closeServer(){
        server.close();
    }

    public String getIPAddress(){
        try {
            byte[] ip = InetAddress.getLocalHost().getAddress();
            String retval = "" + unsigned(ip[0]);
            for(int i = 1; i < ip.length; i++){
                retval += ".";
                retval += unsigned(ip[i]);
            }
            return retval;
        }
        catch (UnknownHostException ex) {
            return "unknown";
        }
    }

    private static int unsigned(byte b){
        if(b < 0){
            int val = (int)b;
            val += 256;
            return val;
        }
        return b;
    }
}
